package com.hk.common.utils;

import com.alibaba.fastjson2.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

public class RequestUtils {
	/**
	 * 获得请求参数
	 *
	 * @param request
	 *            请求
	 * @return 请求发起端IP
	 */
	public static <T> T buildPojo(HttpServletRequest request,Class<T> tClass){
		Map<String,Object> params = getQueryParams(request);
		return JSONObject.from(params).toJavaObject(tClass);
	}

	/**
	 *
	 */
	public static Map<String,Object> getQueryParams(HttpServletRequest request){
		Map<String, String[]> map = request.getParameterMap();
		Map<String,Object> params = new HashMap<>(map.size());
		;
		for (Map.Entry<String, String[]> entry: map.entrySet()) {
			int len = entry.getValue().length;
			if(len==1){
				params.put(entry.getKey(),entry.getValue()[0]);
			}else if(len>1){
				params.put(entry.getKey(),entry.getValue());
			}
		}
		return params;
	}

}
